import tkinter as tk
import sys
from datetime import datetime

# 尝试从time.txt文件中读取中考日期和时间
try:
    with open('time.txt', 'r') as file:
        time_str = file.read().strip()
        exam_date = datetime.strptime(time_str, '%Y-%m-%d-%H-%M-%S')
except:
    exam_date = datetime(2024, 6, 14, 0, 0, 0)
# 创建主窗口
root = tk.Tk()
root.title("中考倒计时")

# 设置窗口初始位置和大小
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
window_width = int(screen_width * 0.4)  # 窗口宽度设置为屏幕宽度的40%
window_height = int(screen_height * 0.08)  # 窗口高度设置为屏幕高度的8%
x_coordinate = (screen_width / 2) - (window_width / 2)
y_coordinate = 0  # 将窗口放置在屏幕顶部

root.geometry('%dx%d+%d+%d' % (window_width, window_height, x_coordinate, y_coordinate))

# 禁用窗口的点击和拖动功能
root.overrideredirect(True)  # 移除窗口边框和标题栏

# 保持窗口始终在最前面
#root.wm_attributes("-topmost", 1)

#remaining=int(100) #测试
# 用于更新窗口内容的函数
def update_window_content():
    # 移除所有子控件
    for widget in root.winfo_children():
        widget.destroy()
    current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    # 计算剩余时间
    remaining_time = exam_date - datetime.now()
    remaining_days = remaining_time.days
    remaining_hours, remainder = divmod(remaining_time.seconds, 3600)
    remaining_minutes, remaining_seconds = divmod(remainder, 60)

    # 根据窗口宽度动态调整字体大小
    font_size = int(window_width / 25)  # 假设每20个像素宽度对应1个字体大小

    # 设置字体颜色
    #global remaining #测试
    #remaining=remaining-1 #测试
    #remaining_days=remaining#测试
    if remaining_days >= 60:
        font_color = "black"
    elif remaining_days >= 50:
        start_color = (255, 240, 60)  # 黄色
        end_color = (255, 165, 0)    # 橙色
        ratio = (60 - remaining_days) / 10
        print(ratio)
        ratio = max(0, min(1, ratio))  # 确保ratio在0到1之间
        r = int(start_color[0] * (1 - ratio) + end_color[0] * ratio)
        g = int(start_color[1] * (1 - ratio) + end_color[1] * ratio)
        b = int(start_color[2] * (1 - ratio) + end_color[2] * ratio)
        font_color = f'#{r:02x}{g:02x}{b:02x}'
    elif remaining_days >= 0:
        start_color = (255, 165, 0)  # 橙色
        end_color = (255, 0, 0)  # 红色
        ratio = (50 - remaining_days) / 50
        print(ratio)
        ratio = max(0, min(1, ratio))  # 确保ratio在0到1之间
        r = int(start_color[0] * (1 - ratio) + end_color[0] * ratio)
        g = int(start_color[1] * (1 - ratio) + end_color[1] * ratio)
        b = int(start_color[2] * (1 - ratio) + end_color[2] * ratio)
        font_color = f'#{r:02x}{g:02x}{b:02x}'
    else:
        font_color='black'
        countdown_label = tk.Label(root, text=f"中考倒计时-by00x0-徐\n              ", font=("Helvetica", font_size), fg="black")
        countdown_label.pack()
        #延迟10秒
        root.after(10000, lambda: sys.exit(0))
    # 显示剩余时间
    countdown_label = tk.Label(root, text=f"距离中考还有: {remaining_days}天{remaining_hours}时{remaining_minutes}分\n当前时间:{current_time}", font=("Helvetica", font_size), fg=font_color)
    countdown_label.pack()

    # 每秒更新时间
    root.after(100, update_window_content)

# 调用更新函数
update_window_content()

# 开始Tkinter事件循环
root.mainloop()
